/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import core.Config;
import core.Session;
import core.User;

/**
 *
 * @author Marcin
 */
public class Main extends javax.swing.JFrame {
    private static Menu menu;
    /**
     * Creates new form Main
     */
    public Main() {
        initComponents();
        menu = null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tLogin = new javax.swing.JTextField();
        pfPassword = new javax.swing.JPasswordField();
        bLoginButton = new javax.swing.JButton();
        cRemember = new javax.swing.JCheckBox();
        bConsole = new javax.swing.JButton();
        lLogin = new javax.swing.JLabel();
        lPassword = new javax.swing.JLabel();
        lTitle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Przychodnia @ UJ");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                Main.this.windowOpened(evt);
            }
        });

        tLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoginButtonActionPerformed(evt);
            }
        });

        pfPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoginButtonActionPerformed(evt);
            }
        });

        bLoginButton.setText("Zaloguj");
        bLoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoginButtonActionPerformed(evt);
            }
        });

        cRemember.setLabel("Loguj automatycznie");

        bConsole.setText("Console");
        bConsole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConsoleActionPerformed(evt);
            }
        });

        lLogin.setText("Login:");

        lPassword.setText("Pass:");

        lTitle.setFont(new java.awt.Font("Calibri", 2, 36)); // NOI18N
        lTitle.setText("Przychodnia");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lLogin)
                    .addComponent(lPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tLogin)
                    .addComponent(bLoginButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cRemember, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                .addGap(113, 113, 113))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bConsole)
                    .addComponent(lTitle))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lLogin))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cRemember)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bLoginButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(bConsole)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bLoginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoginButtonActionPerformed
        Session session = new Session(this);
        User user;
        if(session.logIn(tLogin.getText(), new String(pfPassword.getPassword())))
        {
            if(cRemember.isSelected())
            {
                Config conf = new Config();
                conf.addProperty("remember", "true");
                conf.addProperty("login", tLogin.getText());
                conf.addProperty("password", new String(pfPassword.getPassword()));
                conf.saveConfig();
            }
            this.setVisible(false);
            Menu menuTemp = new Menu(this,session.getUser());
            menuTemp.setLockPanels(session.getUser().getAuthorizationID());
            menuTemp.setVisible(true);
        }
    }//GEN-LAST:event_bLoginButtonActionPerformed

    private void bConsoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConsoleActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DB_TOOL().setVisible(true);    
            }
        });
    }//GEN-LAST:event_bConsoleActionPerformed

    private void windowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowOpened
        Config conf = new Config();
        if(conf.loadConfig())
        {
            if(((String)conf.getProperty("remember")).compareTo("true") == 0){
                Session session = new Session(this);
                if(session.logIn((String)conf.getProperty("login"), (String)conf.getProperty("password")))
                {
                    this.setVisible(false);
                    Menu menu = new Menu(this,session.getUser());
                    menu.setLockPanels(session.getUser().getAuthorizationID());
                    menu.setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_windowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bConsole;
    private javax.swing.JButton bLoginButton;
    private javax.swing.JCheckBox cRemember;
    private javax.swing.JLabel lLogin;
    private javax.swing.JLabel lPassword;
    private javax.swing.JLabel lTitle;
    private javax.swing.JPasswordField pfPassword;
    private javax.swing.JTextField tLogin;
    // End of variables declaration//GEN-END:variables
}
